<?php

  class ScaffoldedController extends AppController {
    
    static $model;
    
    function PerformAction ($action) {
      $code = "self::{$action}();";
      eval($code);
    }
    
    function SetModel($model) {
      return self::$model = $model;
    }
    
    function GetModel() {
      if (isset(self::$model)) {
        return self::$model;
      } else {
        trigger_error("<b><u>Internal error!</u></b> Scaffold model not specified!", E_USER_ERROR);
      }
    }
    
    function GetPluralizedModel() {
      return Inflector::Pluralize(self::GetModel());
    }
    
    function GetModelVar() {
      return Inflector::Variable(self::GetModel());
    }
    
    function GetPluralizedModelVar() {
      return Inflector::Pluralize(self::GetModelVar());
    }
    
    function Indent($number_of_spaces) {
      return str_pad('', $number_of_spaces, ' ');
    }
    
    function IndentCode($code, $number_of_spaces = 6) {
      return self::Indent($number_of_spaces) . $code;
    }
    
    function IndentMultilineCode($code, $indents = false) {
      if (!is_array($code)) {
        $code = explode("\n", $code);
      }
      if ($indents) {
        $code = array_map(array("self", "IndentCode"), $code, $indents);
      } else {
        $code = array_map(array("self", "IndentCode"), $code);
      }
      return implode("\n", $code) . "\n";
    }
    
    function index($code_return = false) {
      $model_name = self::GetModel();
      $plur_var_name = self::GetPluralizedModelVar();
      $code[] = "\${$plur_var_name} = {$model_name}::find(all);";
			$code[] = "\$View->assign('{$plur_var_name}', \${$plur_var_name});";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
    
    function show($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
      $code[] = "\${$var_name} = {$model_name}::find(parameter);";
			$code[] = "\$View->assign('{$var_name}', \${$var_name});";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
    
    function add($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
      $code[] = "\${$var_name} = {$model_name}::generate();";
			$code[] = "\$View->assign('{$var_name}', \${$var_name});";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
    
    function create($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
			$code[] = "\${$var_name} = {$model_name}::generate(\$Params['{$var_name}']);";
			$code[] = "if (\${$var_name}->save()) {";
      $code[] = "  self::redirect_to(\"action: index\", \"notice: {$model_name} has been successfully created.\");";
      $code[] = "} else {";
      $code[] = "  self::redirect_to(\"location: back\", \"notice: Can't save {$var_name}! {\${$var_name}->on_save_message()}\");";
      $code[] = "}";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        pc($code);
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
      
    }
    
    function edit($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
      $code[] = "\${$var_name} = {$model_name}::find(parameter);";
			$code[] = "\$View->assign('{$var_name}', \${$var_name});";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
    
    function update($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
      ActiveRecord::load_class(APP_DIR . 'model' . S, $model_name);
      $primary_key = ActiveRecord::ar_get_primary_key($model_name);
			$code[] = "\${$var_name} = {$model_name}::find(parameter);";
			$code[] = "\${$var_name}->set_attributes(\$Params['{$var_name}']);";
			$code[] = "if (\${$var_name}->save()) {";
      $code[] = "  self::redirect_to(\"action: show\", \"parameter: {\${$var_name}->{$primary_key}}\", \"notice: {$model_name} has been successfully updated.\");";
      $code[] = "} else {";
      $code[] = "  self::redirect_to(\"location: back\", \"notice: Can't save {$var_name}! {\${$var_name}->on_save_message()}\");";
      $code[] = "}";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
    
    function delete ($code_return = false) {
      $model_name = self::GetModel();
      $var_name = self::GetModelVar();
      $code[] = "\${$var_name} = {$model_name}::find(parameter);";
      $code[] = "\${$var_name}->Destroy();";
      $code[] = "self::redirect_to(\"location: back\", \"notice: {\${$var_name}->on_save_message()}\");";
      $code = self::IndentMultilineCode($code);
      if ($code_return == false) {
        eval(PreCompiler::Compile($code));
      } else {
        return $code;
      }
    }
  
  }

?>